package com.elasticjob.comfig;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.elasticjob.job.MySimpleJob;
import com.elasticjob.job.MySimpleJob2;
import com.elasticjob.listener.MySimpleJobDistributeListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: TongRui乀
 * @Date: 2019-12-11 16:07
 * @description：
 */
@Configuration
public class ElasticJobConfig {

    /**
     *  定义注册中心
     * @return
     */
    @Bean
    public CoordinatorRegistryCenter zkRegistry(){
        ZookeeperRegistryCenter registryCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("127.0.0.1:2181", "elastic-job"));
        registryCenter.init();
        return registryCenter;
    }

    /**
     *  job 配置
     *
     *  按依赖顺序  LiteJobConfiguration -> JobTypeConfiguration -> JobCoreConfiguration
     *
     *  JobTypeConfiguration
     *      - SimpleJobConfiguration
     *      - DataFlowJobConfiguration
     *      - ScriptJobConfiguration
     *
     * @return
     */
    @Bean(name = "job1")
    public LiteJobConfiguration liteJobConfiguration1(){
        // 任务1
        // CoreConfig
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration
                .newBuilder("test-job", "0/5 * * * * ?", 1)
                // 是否任务失效转移
                .failover(true)
                .shardingItemParameters("0=a")
                .build();


        // TypeConfig   simple dataFlow script XXXJobConfiguration
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, MySimpleJob.class.getCanonicalName());

        return LiteJobConfiguration.newBuilder(simpleJobConfiguration).build();
    }

    /**
     *  定义任务  简单任务
     * @return
     */
    @Bean(name = "job2")
    public LiteJobConfiguration liteJobConfiguration2(){
        // 任务2
        JobCoreConfiguration job2CoreConfig = JobCoreConfiguration.newBuilder("test-job2", "0/5 * * * * ?", 1).build();

        SimpleJobConfiguration simpleJob2Config = new SimpleJobConfiguration(job2CoreConfig, MySimpleJob2.class.getCanonicalName());

        return LiteJobConfiguration.newBuilder(simpleJob2Config).build();
    }

    /**
     *  启动一个任务
     *
     *  JobScheduler(CoordinatorRegistryCenter registryCenter, LiteJobConfiguration configuration, ElasticJobListener jobListener)
     *
     *  param registryCenter  zk配置中心
     *  param configuration   job 配置
     *  param jobListener     job 监听器
     *
     * @param registryCenter
     * @param configuration
     * @return
     */
    @Bean(initMethod = "init")
    public JobScheduler jobScheduler1(CoordinatorRegistryCenter  registryCenter, @Qualifier("job1") LiteJobConfiguration configuration){
        return new JobScheduler(registryCenter, configuration,new MySimpleJobDistributeListener(0L,5000L));
    }

//    @Bean(initMethod = "init")
    public JobScheduler jobScheduler2(CoordinatorRegistryCenter  registryCenter, @Qualifier("job2") LiteJobConfiguration configuration){
        return new JobScheduler(registryCenter, configuration);
    }

}
